package cn.sumpu.app.guoguojie.web.mvc.blog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.sumpu.app.guoguojie.biz.BlogBiz;
import cn.sumpu.app.guoguojie.biz.CodeBiz;
import cn.sumpu.app.guoguojie.biz.CommentBiz;
import cn.sumpu.app.guoguojie.biz.FeedBiz;
import cn.sumpu.app.guoguojie.biz.MemberBiz;
import cn.sumpu.app.guoguojie.domain.Blog;
import cn.sumpu.app.guoguojie.domain.BlogContent;
import cn.sumpu.app.guoguojie.domain.Code;
import cn.sumpu.app.guoguojie.domain.Comment;
import cn.sumpu.app.guoguojie.domain.Feed;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Ext.BlogExt;
import cn.sumpu.app.guoguojie.domain.Ext.CommentExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.IdGenerator;
import cn.sumpu.app.guoguojie.util.PageModel;
import cn.sumpu.app.guoguojie.util.SubstringHTML;

@Controller
public class BlogController {
	@Autowired
	private BlogBiz blogBiz;
	@Autowired
	private CommentBiz commentBiz;
	@Autowired
	private FeedBiz feedBiz;
	@Autowired
	private CodeBiz codeBiz;
	@Autowired
	private MemberBiz memberBiz;

	public String blogList(HttpServletRequest request)
			throws AppBusinessException {
		getSelectCode(request);
		Member member = (Member) request.getSession().getAttribute("member");
		String familyType = request.getParameter("familyType");
		String classname = request.getParameter("classname");
		String pageIndex = request.getParameter("pageIndex");
		int pageSize = 10;
		if (familyType == null) {
			familyType = "myFamilyType";
		}
		if (classname == "") {
			classname = null;
		}
		if (pageIndex == null) {
			pageIndex = "1";
		}
		List<BlogExt> blogExtList = null;
		if (familyType.equals("myFamilyType")) {
			blogExtList = blogBiz.getBlogCountByClass(member.getIdentifier());
		} else {
			blogExtList = blogBiz.getFrientFamilyBlogCountByClass(member
					.getIdentifier());
		}
		int allBlogCount = 0;
		// 分页页数
		int pageCount = 0;
		for (BlogExt blogExt : blogExtList) {
			allBlogCount += blogExt.getBlogCount();
			if (blogExt.getClassname().equals(classname))
				pageCount = (blogExt.getBlogCount() + pageSize - 1) / pageSize;
			if (classname == null)
				pageCount = (allBlogCount + pageSize - 1) / pageSize;

		}
		request.setAttribute("pageCount", pageCount);
		BlogExt blogExt = new BlogExt();
		blogExt.setMember(member.getIdentifier());
		blogExt.setClassname(classname);
		PageModel pageModel = blogBiz.getPageModel(blogExt,
				Integer.parseInt(pageIndex), familyType);
		request.setAttribute("bList", pageModel.getList());
		request.setAttribute("classname", classname);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("allBlogCount", allBlogCount);
		request.setAttribute("loginMember", member);
		request.setAttribute("familyType", familyType);
		request.setAttribute("blogExtList", blogExtList);
		return null;
	}

	@RequestMapping(value = "/enterBlog.htm")
	public String enterBlogInfo(HttpServletRequest request)
			throws AppBusinessException {
		String operationType = request.getParameter("operationType");
		if (operationType != null) {
			request.setAttribute("operationType", operationType);
		}
		blogList(request);
		return "blog/blog";
	}

	@RequestMapping(value = "/enterPostBlog.htm")
	public String enterPostBlog(HttpServletRequest request)
			throws AppBusinessException {
		getSelectCode(request);
		return "blog/postBlog";
	}

	@RequestMapping(value = "/postBlog.htm")
	public String postBlog(@ModelAttribute("blog") Blog blog, String content,
			HttpServletRequest request) throws AppBusinessException {
		Member member = (Member) request.getSession().getAttribute("member");
		String memberId = member.getIdentifier();
		String identifier = IdGenerator.createIdentifier();
		// 添加日志
		blog.setIdentifier(identifier);
		blog.setMember(memberId);
		// 添加日志内容
		BlogContent blogContent = new BlogContent();
		blogContent.setBlog(identifier);
		blogContent.setMember(memberId);
		blogContent.setContent(content);
		blogContent.setPostIp(request.getRemoteAddr());
		blog.setBlogContent(blogContent);
		// 添加动态
		Feed feed = new Feed();
		feed.setMember(memberId);
		feed.setTitleData(blog.getSubject());
		feed.setFeedId(identifier);
		feed.setFeedType("blog");
		feed.setFeedIdType("blogid");
		feed.setBodyData(getSummeryContent(content));
		feed.setFamily(member.getFamily());
		try {
			blogBiz.addBlog(blog);
			feedBiz.insert(feed);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "blog/blog";
	}

	// 获取文章内容摘要
	private String getSummeryContent(String content) {
		SubstringHTML substringHTML = new SubstringHTML();
		String summeryContent = substringHTML.getNoHTMLString(content, 50);
		return summeryContent;
	}

	@RequestMapping(value = "/delBlog.htm")
	public String delBlog(HttpServletRequest request, String id)
			throws AppBusinessException {
		blogBiz.DeleteBlog(id);
		return "blog/blog";
	}

	@RequestMapping(value = "/enterEditBlog.htm")
	public String enterEditBlog(HttpServletRequest request, String id)
			throws AppBusinessException {
		Blog blog = blogBiz.GetBlogById(id);
		request.setAttribute("blog", blog);
		getSelectCode(request);
		return "blog/editBlog";
	}

	@RequestMapping(value = "/editBlog.htm")
	public String editBlog(HttpServletRequest request, String content,
			@ModelAttribute("blog") Blog blog) throws AppBusinessException {
		BlogContent blogContent = new BlogContent();
		blogContent.setContent(content);
		blogContent.setBlog(blog.getIdentifier());
		blog.setBlogContent(blogContent);
		blogBiz.ModifyBlog(blog);
		return "blog/blog";
	}

	@RequestMapping(value = "/readBlog.htm")
	public String readBlog(String blogid, HttpServletRequest request) {
		Blog blog = null;
		try {
			blog = blogBiz.GetBlogById(blogid);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		request.setAttribute("blog", blog);
		return "blog/readBlog";

	}

	@RequestMapping(value = "/enterBlogComment.htm")
	public String enterBlogComment(HttpServletRequest request, String id)
			throws AppBusinessException {

		Blog blog = blogBiz.GetBlogById(id);
		request.setAttribute("blog", blog);
		// 已阅读
		// int viewAmount = blog.getViewAmount();
		// viewAmount++;
		// blog.setViewAmount(viewAmount);
		// blogBiz.ModifyBlog(blog);

		// 加载已有评论
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commentId", id);
		List<CommentExt> cList = commentBiz.getCommentList(map);
		if (cList != null) {
			request.setAttribute("cList", cList);
		}
		// Member member = (Member) request.getSession().getAttribute("member");
		return "blog/commentBlog";
	}

	@RequestMapping(value = "/commentBlog.htm")
	public String commentBlog(HttpServletRequest request, String cmdContent,
			String id) throws AppBusinessException {
		if (cmdContent != null && cmdContent != "") {
			Member member = (Member) request.getSession()
					.getAttribute("member");
			Blog blog = blogBiz.GetBlogById(id);
			int replyAmount = blog.getReplyAmount();
			replyAmount++;
			blog.setReplyAmount(replyAmount);
			blogBiz.ModifyBlog(blog);
			Comment comment = new Comment();
			comment.setMember(member.getIdentifier());
			comment.setContent(cmdContent);
			comment.setUsername(member.getUsername());
			comment.setPostIp(request.getRemoteAddr());
			comment.setCommentId(id);
			comment.setCommentIdType("blogid");
			commentBiz.insert(comment);

		}
		return enterBlogComment(request, id);
	}

	@RequestMapping(value = "/delComment.htm")
	public String delBlogComment(HttpServletRequest request, String id,
			String identifier) throws AppBusinessException {
		Blog blog = blogBiz.GetBlogById(identifier);
		int replyAmount = blog.getReplyAmount();
		replyAmount--;
		blog.setReplyAmount(replyAmount);
		try {
			commentBiz.deleteCommentById(id);
			blogBiz.ModifyBlog(blog);

		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return enterBlogComment(request, identifier);

	}

	// 获取下拉框集合
	private void getSelectCode(HttpServletRequest request)
			throws AppBusinessException {
		List<Code> vList = codeBiz.getCodeListByType("blog_visibility");
		List<Code> cList = codeBiz.getCodeListByType("blog_class");
		request.setAttribute("cList", cList);
		request.setAttribute("vList", vList);
	}

	// 进入他家的日志
	@RequestMapping(value = "/enterTaBlog.htm")
	public String enterTaBlogInfo(HttpServletRequest request, String family)
			throws AppBusinessException {
		String operationType = request.getParameter("operationType");
		request.setAttribute("operationType", operationType);
		family = memberBiz.getMemberListByDoorplate(family).get(0)
				.getIdentifier();
		request.setAttribute("family", family);
		request.setAttribute("reload", request.getParameter("reload"));
		return "blog/taBlog";
	}

	// 他家的日志
	@RequestMapping(value = "/taBlog.htm")
	public String taBlog(HttpServletRequest request, String family)
			throws AppBusinessException {
		try{
			request.setAttribute("familyType", request.getParameter("familyType"));
			getSelectCode(request);
			String familyType = request.getParameter("familyType");
			String classname = request.getParameter("classname");
			String pageIndex = request.getParameter("pageIndex");

			if (familyType == null) {
				familyType = "myFamilyType";
			}
			if (classname == "") {
				classname = null;
			}
			if (pageIndex == null) {
				pageIndex = "1";
			}
			List<BlogExt> blogExtList = null;
			if (familyType.equals("myFamilyType")) {
				blogExtList = blogBiz.getBlogCountByClass(family);
			} else {
				blogExtList = blogBiz.getFrientFamilyBlogCountByClass(family);
			}
			int allBlogCount = 0;
			int pageCount = 0;
			int pageSize = 3;
			for (BlogExt blogExt : blogExtList) {
				allBlogCount += blogExt.getBlogCount();
				if (blogExt.getClassname().equals(classname))
					pageCount = (blogExt.getBlogCount() + pageSize - 1) / pageSize;
				if (classname == null)
					pageCount = (allBlogCount + pageSize - 1) / pageSize;
			}
			BlogExt blogExt = new BlogExt();
			blogExt.setMember(family);
			blogExt.setClassname(classname);
			PageModel pageModel = blogBiz.getPageModel(blogExt,
					Integer.parseInt(pageIndex), familyType);
			request.setAttribute("bList", pageModel.getList());
			request.setAttribute("classname", classname);
			request.setAttribute("pageIndex", pageIndex);
			request.setAttribute("allBlogCount", allBlogCount);
			request.setAttribute("familyType", familyType);
			request.setAttribute("blogExtList", blogExtList);
			request.setAttribute("family", family);
			request.setAttribute("pageCount", pageCount);
			return "blog/taBlogInfo";
		}catch(Exception e){
			e.printStackTrace();
			return "common/error";
		}
	}
	
}
